package org.jeecg.modules.demo.job;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.demo.Constants;
import org.jeecg.modules.demo.kpi.entity.Kpi;
import org.jeecg.modules.demo.kpi.service.IKpiService;
import org.jeecg.modules.demo.tobedone.entity.ReportKpi;
import org.jeecg.modules.demo.tobedone.entity.Tobedone;
import org.jeecg.modules.demo.tobedone.service.impl.ApprovalKpiServiceImpl;
import org.jeecg.modules.demo.tobedone.service.impl.ReportKpiServiceImpl;
import org.jeecg.modules.demo.tobedone.service.impl.TobedoneServiceImpl;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class ReportJob implements Job {
    @Autowired
    private IKpiService kpiService;
    @Autowired
    private ReportKpiServiceImpl reportKpiService;
    @Autowired
    private ApprovalKpiServiceImpl approvalKpiService;
    @Autowired
    private TobedoneServiceImpl  tobedoneService;

    //这是一个定时器，用来定时创建汇报任务和审批任务，每个月的25号的凌晨3点开始创建
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("开始创建汇报任务");
        System.out.println("开始创建审批任务");
        //获取到所有的kpi的id
        List<Kpi> kpiList = kpiService.getAllList();
        //获取到所有的tobedone
        List<Tobedone> tobedoneList = tobedoneService.getAllList();
        System.out.println("获取到所有的kpi"+kpiList);
        for(Kpi kpi : kpiList){
            //根据kpi的id获取到tobedone的kpi_id，然后找类型为年度关键指标，状态为待汇报或者已汇报的记录。
            //看其完成进度是否达到100%，达到continue,否则创建。
            List<String> completionProgressList =reportKpiService.SelectCompletionProgress(kpi.getId());
            System.out.println("获取到完成情况"+completionProgressList);
            ReportKpi reportkpi = new ReportKpi();
            Date date = new Date();
            int wheatherCreateReportKpi=0;
            for(String completionProgress : completionProgressList){
                if(completionProgress.equals("100%")){
                    wheatherCreateReportKpi=1;
                }
            }
            if(wheatherCreateReportKpi!=1) {
                //对各种字段进行赋值
                System.out.println(Constants.START_ASSIGN);
                reportkpi.setAssessindex(kpi.getAssessindex());
                reportkpi.setReportMonth(String.valueOf(date.getMonth()+1));
                reportkpi.setTaskstatus(Constants.TOBE_REPORT);
                reportkpi.setIndexdefineandtime(kpi.getIndexdefineandtime());
                reportkpi.setCommentrules(kpi.getCommentrules());
                reportkpi.setKpi_id(kpi.getId());
                reportkpi.setTasktype(Constants.KPI);
                System.out.println(reportkpi);
                //一些赋值语句
                reportKpiService.saveReport(reportkpi);
                System.out.println(Constants.SAVE_SUCCESS);
            }
        }
        for(int i=0;i<tobedoneList.size();i++){
            //去找类型为年度关键指标，状态为已汇报的记录。
            List<Tobedone> approvalTobedoneList=tobedoneService.SelectFinishedReportTobedone();
            for(Tobedone taskstatus:approvalTobedoneList)
            {
                if(taskstatus.getTaskstatus().equals(Constants.
                        REPORTED)){
                    approvalKpiService.updateTaskstatusByTid(taskstatus.getId());
                }
            }
        }
    }
}
